const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
const baseUrl = require('../controllers/controllers.js').baseUrl;
Page({
  data: {
    type:'',
    name:'',
    certfront:'',
    certback:'',
    describe:'',
    isselect:false,
    isrelease:false,
    audited:false,
    infodata:'',
    known:'',
    price:'',
    iscomfirm:false,
  },
  onLoad(option) {
    this.lastApproveInfo()
  },
  lastApproveInfo(){
    Controller.lastApproveInfo().then(result => {
      if(result.data.baseData){
        this.setData({
          infodata:result.data.baseData,
          type:result.data.baseData.subRoleType,
          name:result.data.baseData.name,
          describe:result.data.baseData.otherContent,
          certfront:result.data.baseData.certImgUrls?result.data.baseData.certImgUrls.split(',')[0]:'',
          certback:result.data.baseData.certImgUrls?result.data.baseData.certImgUrls.split(',')[1]:'',
        })
      }
    });
  },
  
  changetype(e){
    this.setData({
      type:e.currentTarget.dataset.type,
    })
  },
  release(){
    Controller.appinfo({key:'key-approve-price'}).then(result => {
      if(result.data.baseData){
        this.setData({
          price:result.data.baseData.content,
          isrelease:true
        })
      }
    });
    Controller.appinfo({key:'key-approve-notice'}).then(result => {
      if(result.data.baseData){
        this.setData({
          known:result.data.baseData.content,
          isrelease:true
        })
      }
    });
  },
  cancel(){
    this.setData({
      isrelease:false
    })
  },
  golink(e){
    var url=e.currentTarget.dataset.url;
    if (!wx.getStorageSync('logintoken')) {
      app.goLogin(false)
      return
    }
    wx.navigateTo({
      url:e.currentTarget.dataset.url
    });
  },
  changeinput(e){
    let data = {};
    data[e.currentTarget.dataset.name]=e.detail.value
    this.setData(data)
  },
  upShopLogo: function (e) {
    var that = this;
    var name=e.currentTarget.dataset.name;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#222",
      success: function (res) {
        if(!res.cancel) {
          if(res.tapIndex == 0) {
            that.chooseWxImageShop('album',name);//从相册中选择
          }else if(res.tapIndex == 1) {
            that.chooseWxImageShop('camera',name);//手机拍照
          }
        }
      }
    })
  },
  chooseWxImageShop: function (type,name) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
        success: function (res) {
        var img=res.tempFilePaths[0]
        that.picture(img,name)
      }
    })
  },
  picture(e,name){
    var that=this;
    wx.uploadFile({
        url: baseUrl+'/upload/picture',
        filePath: e,
        name: "file",
        formData: {
        },
        success: function (res) {
          let data = {};
          data[name]=JSON.parse(res.data).data.baseData[0]
          that.setData(data)
        }
    })
  },
  comfirm(){
    if(!this.data.type){
      wx.showToast({
          title: '请选择类型',
          icon: 'none'
      });
      return
    }
    if(!this.data.name){
      wx.showToast({
          title: '请输入您的称呼',
          icon: 'none'
      });
      return
    }
    if(!this.data.certfront){
      wx.showToast({
          title: '请上传身份证正面',
          icon: 'none'
      });
      return
    }
    if(!this.data.certback){
      wx.showToast({
          title: '请上传身份证背面',
          icon: 'none'
      });
      return
    }
    if(!this.data.describe){
      wx.showToast({
          title: '填写补充信息',
          icon: 'none'
      });
      return
    }
    var data={
      subRoleType:this.data.type,
      otherContent:this.data.describe,
      certImgUrls:this.data.certfront+','+this.data.certback,
      name:this.data.name
    }
    if(this.data.iscomfirm){
      return
    }
    this.setData({
      iscomfirm:true
    })
    Controller.approve(data).then(result => {
      this.setData({
        iscomfirm:false
      })
      this.createWxPay(result.data.baseData)
    });
    
  },
  createWxPay(orderNumber) {
    var that=this;
    var obj={}
    obj.orderNumber=orderNumber
    obj.payType=1
    Controller.pay(obj).then(res => {
      let data=res.data.baseData;
      wx.requestPayment({
        nonceStr: data.nonceStr,
        package: data.package,
        paySign: data.paySign,
        timeStamp: data.timeStamp,
        signType: data.signType,
        success(res){
          wx.showToast({
            title: "支付成功",
            icon: 'none',
            duration: 1000
          });
          setTimeout(() => {
            that.setData({
              isrelease:false
            })
            that.lastApproveInfo()
          },1000)
        },
        fail(res){
          wx.showToast({
            title: "支付失败",
            icon: 'none',
            duration: 1000
          });
        }
      })
    }).catch(res=>{
      wx.showToast({
        title: "支付失败",
        icon: 'none',
        duration: 1000
      });
    })
    
  },
})